<template>
  <div class="content">
    <div class="header">
      <h3 class="title">社团列表</h3>
      <el-button type="primary" @click="handleNewUserClick">新建社团</el-button>
    </div>
    <div class="table">
      <el-table :data="clubsList" border style="width: 100%">
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60px"
        />

        <el-table-column
          align="center"
          label="社团名称"
          prop="name"
          width="150px"
        />

        <el-table-column
          align="center"
          label="社长"
          prop="leaderName"
          width="150px"
        />
        <el-table-column align="center" label="社团头像" prop="avatar_url">
          <template #default="scope">
            <img style="width: 50px" :src="scope.row.club_avatar" alt="" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="created_at">
          <template #default="scope">
            {{ formatUTC(scope.row.created_at) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="更新时间" prop="updated_at">
          <template #default="scope">
            {{ formatUTC(scope.row.updated_at) }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="200px">
          <template #default="scope">
            <el-button
              size="small"
              icon="Edit"
              type="primary"
              text
              @click="handleEditBtnClick(scope.row)"
            >
              <!-- @click="handleEditBtnClick(scope.row)" -->
              编辑
            </el-button>
            <el-button
              v-if="role === 2"
              size="small"
              icon="Delete"
              type="danger"
              text
              @click="handleDeleteBtnClick(scope.row.id)"
            >
              删除
            </el-button>
            <el-button
              size="small"
              icon="document"
              type="success"
              text
              @click="handleDetailBtnClick(scope.row.id)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-if="role === 2" class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="clubsTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import useClubStore from '@/store/main/club/club'
import { formatUTC } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { getRole } from '@/utils/handle-role'
const emit = defineEmits(['newClick', 'editClick'])

// 1.发起action，请求usersList的数据
const clubStore = useClubStore()
const currentPage = ref(1)
const pageSize = ref(10)
const role = ref(getRole())

fetchClubListData()
// 2.获取usersList数据,进行展示
const { clubsList, clubsTotalCount } = storeToRefs(clubStore)

// 3.页码相关的逻辑
function handleSizeChange() {
  fetchClubListData()
}
function handleCurrentChange() {
  fetchClubListData()
}

// 4.定义函数, 用于发送网络请求
function fetchClubListData(formData?: any) {
  if (role.value !== 2) {
    clubStore.getClubByUserIdAction()
    return
  }
  // 1.获取offset/size
  const limit = pageSize.value
  const offset = (currentPage.value - 1) * limit
  // 2.发起网络请求
  if (formData) {
    clubStore.getClubListByNameAction(formData.name, limit, offset)
    return
  }
  clubStore.getClubsListAction(limit, offset)
}

// 5.删除数据
async function handleDeleteBtnClick(id: number) {
  ElMessageBox.confirm(`确认删除这个社团吗`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await clubStore.deleteClubByIdAction(id)
    ElMessage({
      type: 'success',
      message: '删除社团成功',
      duration: 1000
    })
  })
}
function handleNewUserClick() {
  emit('newClick')
}
function handleEditBtnClick(itemData: any) {
  emit('editClick', itemData)
}

const router = useRouter()
function handleDetailBtnClick(id: number) {
  router.push({
    path: '/main/club/detail',
    query: {
      id
    }
  })
}

defineExpose({
  fetchClubListData
})
</script>

<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;

  .title {
    font-size: 22px;
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
